from django.template import Context, loader
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse
from list.models import Server
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
import time

@login_required(login_url='/accounts/login/')
def index(request):
	serverlist = Server.objects.all()
	use = []
	for server in serverlist:
		if server.time >= time.time() - 120: #Must been seen last 120 seconds to be shown
			use.append(server)
	t = loader.get_template('list/index.html')
	c = Context({'serverlist':use})
	return HttpResponse(t.render(c))

@login_required(login_url='/accounts/login/')
def load(request, serverid):
	p = get_object_or_404(Server, id=serverid)	
	t = loader.get_template('list/load.jnlp')
	if request.user.is_authenticated():
		c = Context({'path':'load/'+str(serverid)+'/'+str(request.user.username),'server':p.ipaddress,'name':request.user.username})
	else:
		c = Context({'path':'load/'+str(serverid)+'/DUMMYNAME','server':p.ipaddress,'name':'DUMMYNAME'})	
	return HttpResponse(t.render(c), content_type='application/x-java-jnlp-file')

def loadafter(request, serverid, name):
	p = get_object_or_404(Server, id=serverid)	
	t = loader.get_template('list/load.jnlp')
	if request.user.is_authenticated():
		c = Context({'path':'load/'+str(serverid)+'/'+str(name),'server':p.ipaddress,'name':request.user.username})
	else:
		c = Context({'path':'load/'+str(serverid)+'/'+str(name),'server':p.ipaddress,'name':name})	
	return HttpResponse(t.render(c), content_type='application/x-java-jnlp-file')

@login_required(login_url='/accounts/login/')
def startnew(request):
	t = loader.get_template('list/load.jnlp')
	if request.user.is_authenticated():
		c = Context({'path':'start/'+str(request.user.username),'server':request.META['REMOTE_ADDR'],'is_server':'true','name':request.user.username})
	else:
		c = Context({'path':'start/'+str('DUMMYNAME'),'server':request.META['REMOTE_ADDR'],'is_server':'true','name':'DUMMYNAME'})	
	return HttpResponse(t.render(c), content_type='application/x-java-jnlp-file')

def startnewafter(request, name):
	t = loader.get_template('list/load.jnlp')
	if request.user.is_authenticated():
		c = Context({'path':'start/'+str(name),'server':request.META['REMOTE_ADDR'],'is_server':'true','name':request.user.username})
	else:
		c = Context({'path':'start/'+str(name),'server':request.META['REMOTE_ADDR'],'is_server':'true','name':name})	
	return HttpResponse(t.render(c), content_type='application/x-java-jnlp-file')

@csrf_exempt
def register(request):
	if not request.method == "POST":
		return HttpResponse('Must use POST method...')
	
	serv = request.POST['servername']
	add = request.META['REMOTE_ADDR']

	try:
		ex = Server.Objects.get(servername=serv, ipaddress=add)
		ex.time = time.time()
		ex.save()
	except:	
		s = Server(servername=serv, ipaddress=add, time=time.time())
		s.save()	

	return HttpResponse('OK')

@csrf_exempt
def renew(request):
        if not request.method == "POST":
                return HttpResponse('Must use POST method...')

        serv = request.POST['servername']
        add = request.META['REMOTE_ADDR']

        s = get_object_or_404(Server, servername=serv, ipaddress=add)
        s.time = time.time()
	s.save()

        return HttpResponse('OK')

@csrf_exempt
def unregister(request):
        if not request.method == "POST":
                return HttpResponse('Must use POST method...')

        serv = request.POST['servername']
        add = request.META['REMOTE_ADDR']
	
        s = get_object_or_404(Server, servername=serv, ipaddress=add)
        s.delete()

        return HttpResponse('OK')
